﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace The_LarX
{
    public class FireNumberDrawer3D : Component
    {
        public CustomParticleSystem FireParticles;
        public CustomParticleSystem SmokeParticles;

        private NumberDrawer3D Drawer;

        public Renderer Renderer;

        public FireNumberDrawer3D(string zero,
            string one, string two, string three, string four,
            string five, string six, string seven, string eight,
            string nine, Renderer renderer, GameScreen Screen)
            : base(Screen) 
        {
            this.Renderer = renderer;
            FireParticles = new CustomParticleSystem(renderer, 25, Screen);
            FireParticles.TextureName = "Engine\\Textures\\ParticleSystem\\tex_fire";
            FireParticles.Actor.Visible = false;
            FireParticles.Position = Vector3.Zero;
            FireParticles.ParticlesPerSecond = 25;
            FireParticles.Active = true;
            FireParticles.StartGravity = new Vector3(0, 2, 0);
            FireParticles.Gravity = new Vector3(0, 2, 0);
            FireParticles.MaxHorizontalVelocity = 1;
            FireParticles.MinHorizontalVelocity = -1;
            FireParticles.MaxVerticalVelocity = 3;
            FireParticles.MinVerticalVelocity = 0;
            FireParticles.Duration = 1;
            FireParticles.EmitterVelocitySensitivity = 0;

            FireParticles.MinStartSize = 1;
            FireParticles.MaxStartSize = 3;
            FireParticles.MinEndSize = 1;
            FireParticles.MaxEndSize = 4;

            SmokeParticles = new CustomParticleSystem(renderer, 25, Screen);
            SmokeParticles.TextureName = "Engine\\Textures\\ParticleSystem\\tex_smoke";
            //SmokeParticles.Actor.SceneObject.Visibility = ObjectVisibility.None;
            SmokeParticles.Position = Vector3.Zero;
            SmokeParticles.ParticlesPerSecond = 12.5f;
            SmokeParticles.Active = true;
            SmokeParticles.StartGravity = new Vector3(0, 3, 0);
            SmokeParticles.Gravity = new Vector3(0, -1, 0);
            SmokeParticles.MaxHorizontalVelocity = 1;
            SmokeParticles.MinHorizontalVelocity = -1;
            SmokeParticles.MaxVerticalVelocity = 3;
            SmokeParticles.MinVerticalVelocity = 0;
            SmokeParticles.Duration = 2;
            SmokeParticles.EmitterVelocitySensitivity = 0;

            SmokeParticles.MinStartSize = 1;
            SmokeParticles.MaxStartSize = 3;
            SmokeParticles.MinEndSize = 1;
            SmokeParticles.MaxEndSize = 4;

            Drawer = new NumberDrawer3D(zero, one, two, three, four,
                                        five, six, seven, eight, nine, renderer);

            foreach (KeyValuePair<char, Actor> actor in Drawer.Dictionary)
            {
                actor.Value.SubActors[0].DiffuseColor = Color.Red.ToVector4();
            }
        }

        public void SetTexture(string tex)
        {
            foreach (KeyValuePair<char, Actor> actor in Drawer.Dictionary)
            {
                actor.Value.SubActors[0].TextureName = tex;
            }
        }

        public override void Update()
        {
            base.Update();

            FireParticles.Update();
            SmokeParticles.Update();
        }

        public void DrawNumbers(string Numbers, Vector3 Scale, Vector3 DegressRotation, Vector3 Position, Vector3 Direction)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Actor Actor = Drawer.Dictionary[Numbers[i]];
                Actor.Scale = Scale;
                Actor.Position = Position;
                Actor.Position -= Direction * i;
                Actor.DegreesRotation = DegressRotation;
                Actor.Update();
                Actor.Draw();
                float posX = Actor.Position.X + (float)(RandomUtil.Next(-(int)(float)(Actor.Scale.X * 15), (int)(float)(Actor.Scale.X * 15))) 
                    / 10f;
                float posZ = Actor.Position.Z + (float)(RandomUtil.Next(-(int)Actor.Scale.Z, (int)Actor.Scale.Z));
                FireParticles.Actor.Position = new Vector3(posX,
                    Actor.Position.Y - Actor.Scale.Y / 2,
                    posZ);
                SmokeParticles.Update();
                SmokeParticles.Actor.Position = new Vector3(posX,
                    Actor.Position.Y + 1 - Actor.Scale.Y / 2,
                    posZ);
                Renderer.SubmitActor(Actor);
                FireParticles.Emmiter.Update(FireParticles.Actor.Position);
                SmokeParticles.Emmiter.Update(SmokeParticles.Actor.Position);
                FireParticles.Draw();
                SmokeParticles.Draw();
            }
        }
    }
}
